<?php

namespace Admin\Controller;

use Think\Controller;
use Think\Page;

class PaylogController extends Controller {

    public function __construct() {
        parent::__construct();
        session("[start]");
        if (!isset($_SESSION['admininfo']) || $_SESSION['admininfo']['id'] < 1) {
            $this->redirect('Login/index');
        }
    }

    public function index() {
        
        $order_num = $_REQUEST['order_num'] ? trim(I('request.order_num')) : '';
        $payid = $_REQUEST['payid'] ? intval($_REQUEST['payid']) : 0;
        $game_id = $_REQUEST['game_id'] ? intval($_REQUEST['game_id']) : 0;
        $area_num = $_REQUEST['area_num'] ? intval($_REQUEST['area_num']) : 0;
        $username = $_REQUEST['username'] ? trim(I('request.username')) : '';
        $dt1 = $_REQUEST['data_start'] ? I('request.data_start') : '';
        $dt2 = $_REQUEST['data_end'] ? I('request.data_end') : '';

        $gameModel = new \Admin\Model\GameModel();
        $gamelist = $gameModel->getGameList();

        $paytypemodel = M('paytype');
        $paytype = $paytypemodel->select();


        $paymodel = new \Admin\Model\PaylogModel();
        $log = $paymodel->getLoglist($order_num, $payid, $game_id, $area_num, $username, strtotime($dt1), strtotime($dt2));
        $count = count($log);
        //分页跳转参数
        $parameter=array(
            'game_id'=>$game_id,
            'area_num'=>$area_num,
            'order_num'=>$order_num,
            'payid'=>$payid,
            'username'=>$username,
            'data_start'=>$dt1,
            'data_end'=>$dt2,
        );
        $Page = new Page($count, 10,$parameter); // 实例化分页类 传入总记录数
        $show = $Page->show(); // 分页显示输出
        $list = $paymodel->getLoglist($order_num, $payid, $game_id, $area_num, $username, strtotime($dt1), strtotime($dt2), $Page->firstRow, $Page->listRows);//充值列表

        //注册来源
        $referModel=M("cps_member");
        $referlist=$referModel->where('pid<>0 and refer<>0')->select();
        for($i=0;$i<count($list);$i++){
            for($j=0;$j<count($referlist);$j++){
                $temp=  explode(',', $referlist[$j]['refer']);
                if(in_array($list[$i]['refer'],$temp)){
                    $list[$i]['refername']=$referlist[$j]['username'];
                }
            }
        }

        $this->assign('paytype', json_encode($paytype));
        $this->assign('game_id', $game_id);
        $this->assign('area_num', $area_num);
        $this->assign("username", $username);
        $this->assign('payid', $payid);
        $this->assign('order_num', $order_num);
        $this->assign('dt1', $dt1);
        $this->assign('dt2', $dt2);
        $this->assign('gamelist', json_encode($gamelist));
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->display();
    }

    public function serverlist($game_id) {
        if (IS_AJAX) {
            $game_id = intval($_GET['game_id']);
            $serverModel = M('game_server_list');
            $server = $serverModel->field("area_num,areaname")->where("game_id='%d'", $game_id)->select();
            $this->ajaxReturn($server, 'json');
        }
    }

}
